﻿
var retDBData = new Array();
//----------初始化 ----------------  
function btnBack_Click() {
    var url = "/JobTask/List?dbName=" + encodeURI(UrlArray["dbName"]);
    if (UrlArray["DBType"] == "2") {
        url += "&DBType=" + UrlArray["DBType"];
    }
    window.location.href = url;
}
function SelectDate(id) {
    $('#' + id).datebox('enable');
}
function bingDropdownList() {
    for (var t = 0; t <= 23; t++) {
        $('.sel_TimeH').append("<option value='" + t + "'>" + t + "</option>");
    }
    for (var t = 0; t <= 59; t = t + 5) {
        $('.sel_TimeM').append("<option value='" + t + "'>" + t + "</option>");
    }
    $(".IntervalM").val("5");
    $(".IntervalTH").val("23");
    $(".IntervalTM").val("55");
    $('.sel_TimeH').width(50);
    $('.sel_TimeM').width(50);
}

function checkNum(v) {
    v = v.replace(/\D/g, '');
    v = parseInt(v);
    if (v > 0)
        return v;
    else
        return "0";
}

//----------初始化 END----------------

//----------提交数据 ----------------
function submitForm() {
    if ($('#ff').form("validate")) {
        if ($.trim($('#txt_TaskName').val()) == "") {
            alert("任务名称不能为空！");
            return false;
        }
        var WeekDay = new Array();
        $(".WeekDay").each(function (i, d) {
            if (d.checked == true) {
                WeekDay.push(d.value);
            }
        });
        var wtDays = 0;
        switch ($.trim($('#sel_TaskPlanType').val())) {
            case "1":
                if (jv.trimValue('.TaskIntervalDays') == "" || jv.trimValue('.TaskIntervalDays') == "0") {
                    alert("请输入有效的执行周期！");
                    return false;
                }
                wtDays = jv.trimValue('.TaskIntervalDays');
                break;
            case "2":
                if (jv.trimValue('.TaskIntervalWeeks') == "" || jv.trimValue('.TaskIntervalWeeks') == "0" ||
                WeekDay.length == 0) {
                    alert("请输入有效的执行周期！");
                    return false;
                }
                wtDays = jv.trimValue('.TaskIntervalWeeks') * 7;
                break;
            case "3":
                if (jv.trimValue('.PlanType') == "1" && (jv.trimValue('.DayForMonth') == "" ||
                jv.trimValue('.TaskIntervalMonth') == "")) {
                    alert("请输入有效的执行周期！");
                    return false;
                }
                if (jv.trimValue('.PlanType') == "2" && jv.trimValue('.TaskIntervalMonth2') == "") {
                    alert("请输入有效的执行周期！");
                    return false;
                }
                wtDays = jv.trimValue('.TaskIntervalMonth') * 28;
                break;
        }
        var ftype = $('input[name="rad_FType"]:checked').val();
        var FHM;
        var THM;

        //等候时间
        var waitt = parseInt($('#txt_WaitTimesH').val() * 3600) + parseInt($('#txt_WaitTimesM').val() * 60) + parseInt($('#txt_WaitTimesS').val());
        if (ftype == "0") {
            $('#txt_TaskStartTime').val($('.OnceH').val() + ":" + $('.OnceM').val() + ":00");
            if (wtDays * 24 * 3600 <= waitt) {
                alert("等候时间必须小于执行周期！");
                return false;
            }
        }
        else {
            var dtm = parseInt($('.IntervalH').val() * 60) + parseInt($('.IntervalM').val()); //执行间隔时间
            if (dtm < 5) {
                alert("任务执行间隔时间不能为0！");
                return false;
            }
            if (dtm * 60 <= waitt) {
                alert("等候时间必须小于执行间隔时间！");
                return false;
            }
            $('#txt_TaskEveryDayIntervalMinute').val(dtm);
            $('#txt_TaskStartTime').val($('.IntervalFH').val() + ":" + $('.IntervalFM').val() + ":00");
            FHM = parseInt($('.IntervalFH').val() * 60) + parseInt($('.IntervalFM').val());
            THM = parseInt($('.IntervalTH').val() * 60) + parseInt($('.IntervalTM').val());
            if (FHM >= THM) {
                alert("任务执行间隔结束时间必须大于开始时间！");
                return false;
            }

        }
        $('#txt_FrequencyType').val(ftype);
        $('#txt_TaskEndTime').val($('.IntervalTH').val() + ":" + $('.IntervalTM').val() + ":00");

        $('#txt_TaskStartDate').val($(".txt_TaskStartDate").datebox('getValue'));
        $('#txt_TaskEndDate').val($(".txt_TaskEndDate").datebox('getValue'));
        //
        var arr_sdate = $('#txt_TaskStartDate').val().split("-");
        var starttime = new Date(arr_sdate[0], arr_sdate[1], arr_sdate[2]);
        var starttimes = starttime.getTime();

        var arr_edate = $('#txt_TaskEndDate').val().split("-");
        var lktime = new Date(arr_edate[0], arr_edate[1], arr_edate[2]);
        var lktimes = lktime.getTime();

        if (arr_sdate.length != 3 || arr_edate.length != 3) {
            alert("日期格式错误。");
            return false;
        }
        if (starttimes > lktimes) {
            alert("失效日期必须大于生效日期。");
            return false;
        }
        var userIDs = new Array();
        for (var i = 0; i < UserDataList.length; i++) {
            userIDs.push(UserDataList[i].Guid);
        }
        var modelIDs = new Array();
        for (var i = 0; i < ModelDataList.length; i++) {
            modelIDs.push(ModelDataList[i].GUID);
        }
        var groupIDs = new Array();
        for (var i = 0; i < GroupDataList.length; i++) {
            groupIDs.push(GroupDataList[i].GUID);
        }
        $("#txt_WaitTimes").val($("#txt_WaitTimesH").val() + ":" + $("#txt_WaitTimesM").val() + ":" + $("#txt_WaitTimesS").val());
        var postUrl = "/JobTask/Update?dbName=" + encodeURI(UrlArray["dbName"]);
        var TaskPlan = jv.GetDivJson($(".TaskPlan"), 0);
        $.ajax({
            type: "POST",
            async: true,
            url: postUrl,
            data: { postData: jv.GetDivJson($(".table_bg"), 4), TaskPlan: TaskPlan, WeekDay: WeekDay.join(","),
                UserList: userIDs.join(","), ModelList: modelIDs.join(","), GroupList: groupIDs.join(",")
            },
            success: function (result) {
                if (result.Message) {
                    if (result.Success == true) {
                        alert(result.Message);
                        btnBack_Click();
                    }
                    else {
                        alert(result.Message);
                    }
                } else
                    alert(result);
            }
        });
    }
}


//----------提交数据 END----------------

//----------联系人信息 ----------------
var UserDataList = new Array();
function AddUser() {
    showModalMany('Contact', '/SystemConfiguration/UserList?dbName=' + encodeURI(UrlArray["dbName"]), '900', '500', GetAddUser);
}
function GetAddUser(v) {
    for (var i = 0; i < v.length; i++) {
        var add = true;
        for (var j = 0; j < UserDataList.length; j++) {
            if (UserDataList[j].Guid == v[i].Guid) {
                add = false;
            }
        }
        if (add) {
            UserDataList.push(v[i]);
        }
    }
    var data = { "total": 0, "rows": UserDataList };
    UserOpts["data"] = data;
    $("#dataGridUser").dg(UserOpts);
}
var UserOpts = {
    height: 380,
    rows: 100,
    idField: "Guid",
    para: { TypeID: UrlArray["id"] },
    checkbox: false,
    columns: [
                    { field: 'EmployeeName', title: '姓名', width: 10 },
                    { field: 'Username', title: '用户名', width: 10 },
                    { field: 'CompanyName', title: '公司', width: 35 },
                    { field: 'OfficeTel', title: '办工电话', width: 15 },
                    { field: 'OfficeEmail', title: 'Email', width: 20 },
                    { field: 'Guid', title: '操作', width: 10, align: 'center',
                        formatter: function (value, rowData, rowIndex) {
                            var add = true; ;
                            for (var j = 0; j < UserDataList.length; j++) {
                                if (UserDataList[j].Guid == value) {
                                    add = false;
                                }
                            }
                            if (add) {
                                UserDataList.push(rowData);
                            }
                            return "<a href='#' onclick='removeUserData(\"" + rowIndex + "\")'>删除</a>";
                        }
                    }
                    ]
};
function removeUserData(v) {
    if (confirm("确定要删除吗？")) {
        UserDataList.removeAt(v);
        var data = { "total": 0, "rows": UserDataList };
        UserOpts["data"] = data;
        $("#dataGridUser").dg(UserOpts);
    }
}
//----------联系人信息 END----------------

//----------加工明细列表 ----------------
var ModelDataList = new Array();
function AddModel() {
    if (UrlArray["DBType"] == 2)
        showModalMany('AddItem', '/ODSConfig/SourceList?dbName=' + encodeURI(UrlArray["dbName"]), '900', '480', GetAddModel);
    else
        showModalMany('Model', '/ModelConfig/ModelList?step=6&dbName=' + encodeURI(UrlArray["dbName"]), '900', '500', GetAddModel);
}
function GetAddModel(v) {
    for (var i = 0; i < v.length; i++) {
        var add = true;
        var obj = new Object();
        if (UrlArray["DBType"] == 2) {
            obj.GUID = v[i].GUID;
            obj.Name = v[i].DisplayName;
            obj.Description = v[i].ViewChnName;
        }
        else {
            obj.GUID = v[i].GUID;
            obj.Name = v[i].DefaultView;
            obj.Description = v[i].TableDescription;
            obj.vwName = v[i].vwName;
        }
        for (var j = 0; j < ModelDataList.length; j++) {
            if (ModelDataList[j].GUID == obj.GUID) {
                add = false;
            }
        }
        if (add) {
            ModelDataList.push(obj);
        }
    }
    var data = { "total": 0, "rows": ModelDataList };
    ModelOpts["data"] = data;
    $("#dataGridModel").dg(ModelOpts);
}
var ModelOpts = {
    height: 380,
    rows: 100,
    idField: "GUID",
    para: { TypeID: UrlArray["id"] },
    checkbox: false,
    columns: [
                    { field: 'Name', title: '已保存明细', width: 30 },
                    { field: 'Description', title: '明细说明', width: 30 },
                    { field: 'vwName', title: '源系统视图名', width: 30 },
                    { field: 'GUID', title: '操作', width: 10, align: 'center', formatter: function (value, rowData, rowIndex) {
                        var add = true; ;
                        for (var j = 0; j < ModelDataList.length; j++) {
                            if (ModelDataList[j].GUID == value) {
                                add = false;
                            }
                        }
                        if (add) {
                            ModelDataList.push(rowData);
                        }
                        return "<a href='#' onclick='removeModelData(\"" + rowIndex + "\")'>删除</a>";
                    }
                    }
                    ]
};
function removeModelData(v) {
    if (confirm("确定要删除吗？")) {
        ModelDataList.removeAt(v);
        var data = { "total": 0, "rows": ModelDataList };
        ModelOpts["data"] = data;
        $("#dataGridModel").dg(ModelOpts);
    }
}
//----------加工明细列表 END----------------

//----------推送明细列表 ----------------
var GroupDataList = new Array();
function AddGroup() {
    var url = '/ViewGroup/SelectGroupList?dbName=' + encodeURI(UrlArray["dbName"]);
    showModalMany('Contact', url, '900', '500', GetAddGroup);
}
function GetAddGroup(v) {
    for (var i = 0; i < v.length; i++) {
        var add = true;
        var obj = new Object();
        obj.GUID = v[i].GroupID;
        obj.Name = v[i].GroupName;
        obj.Description = v[i].Description;
        obj.TargetSysName = v[i].TargetSysName;
        for (var j = 0; j < GroupDataList.length; j++) {
            if (GroupDataList[j].GUID == obj.GUID) {
                add = false;
            }
        }
        if (add) {
            GroupDataList.push(obj);
        }
    }
    var data = { "total": 0, "rows": GroupDataList };
    GroupOpts["data"] = data;
    $("#dataGridGroup").dg(GroupOpts);
}
var GroupOpts = {
    height: 380,
    rows: 100,
    idField: "GUID",
    para: { TypeID: UrlArray["id"] },
    checkbox: false,
    columns: [
                    { field: 'Name', title: '已保存明细', width: 30 },
                    { field: 'Description', title: '明细说明', width: 30 },
                    { field: 'TargetSysName', title: '对接系统名称', width: 30 },
                    { field: 'GUID', title: '操作', width: 10, align: 'center', formatter: function (value, rowData, rowIndex) {
                        var add = true; ;
                        for (var j = 0; j < GroupDataList.length; j++) {
                            if (GroupDataList[j].GUID == value) {
                                add = false;
                            }
                        }
                        if (add) {
                            GroupDataList.push(rowData);
                        }
                        return "<a href='#' onclick='removeGroupData(\"" + rowIndex + "\")'>删除</a>";
                    }
                    }
                    ]
};
function removeGroupData(v) {
    if (confirm("确定要删除吗？")) {
        GroupDataList.removeAt(v);
        var data = { "total": 0, "rows": GroupDataList };
        GroupOpts["data"] = data;
        $("#dataGridGroup").dg(GroupOpts);
    }
}

//----------推送明细列表 END----------------
function checkNumAndValue(input, v) {
    var vv = checkNum(input);
    if (vv > v)
        return v;
    else
        return vv;
}
function GetTaskPlanType(v) {
    switch (v) {
        case "1":
            $("#IntervalDay").show();
            $("#IntervalWeek").hide();
            $("#IntervalMonth").hide();
            break;
        case "2":
            $("#IntervalDay").hide();
            $("#IntervalWeek").show();
            $("#IntervalMonth").hide();
            break;
        case "3":
            $("#IntervalDay").hide();
            $("#IntervalWeek").hide();
            $("#IntervalMonth").show();
            break;
    }
}